<?php

/**
 * @功能 函数库
 */
    header("Content-type:text/html;charset=utf-8");
    /**
     * 
     * @param string $ip ip地址
     * @return string 返回ip地址对应的位置信息
     */
function position($ip = "") {

    $ch = curl_init();
    $str = 'http://ip.taobao.com/service/getIpInfo.php?ip=' . $ip;
    curl_setopt($ch, CURLOPT_URL, $str);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $data = curl_exec($ch);
    curl_close($ch);
    $response = json_decode($data, JSON_UNESCAPED_UNICODE);
    if ($response['code'] === 0) {
        $data = $response['data'];
        $position = $data['country'] . $data['region'] . $data['city'] . $data['isp'];
        return $position;
    } else {
        return "保密";
    }
}
/**
 * 
 * @param string $info 发送的信息
 * @param string $userid 用户id使用ip做id
 * @return string 回复的信息
 */
function replay($info = "", $userid = "") {
    
 $ch = curl_init("http://www.tuling123.com/openapi/api");
 curl_setopt($ch, CURLOPT_POST, 1);     
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $data = array(
        "key" => "f282ae68fb294d0bbb8f66f7ad26ccf8",
        "info" => "$info",
        "userid" => "$userid"
    );
    $sendstr = json_encode($data);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
      'Content-Type: application/json; charset=utf-8',
      'Content-Length: ' . strlen($sendstr)
    ));
    curl_setopt($ch, CURLOPT_POSTFIELDS ,$sendstr );
    $receive = curl_exec($ch);  
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    $data = json_decode($receive);
    $receive = $data->text;
    return $receive;
}

